

<div id="div">


</div>

<input type="text" id="content" name="content"><button onclick="changes('user.php')">send</button>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
<script>
    // 连接服务端，workerman.net:2120换成实际部署web-msg-sender服务的域名或者ip
    var socket = io('http://192.168.1.66:2120');
    // uid可以是自己网站的用户id，以便针对uid推送以及统计在线人数
    uid = 123;
    // socket连接后以uid登录
    socket.on('connect', function(){
    	socket.emit('login', uid);
    });
    // 后端推送来消息时
    socket.on('new_msg', function(msg){
    	var node=document.createElement("p");
		var textnode=document.createTextNode('receive:  '+msg);
		node.appendChild(textnode);
    	document.getElementById('div').appendChild(node);
        console.log("收到消息："+msg);
    });
    // 后端推送来在线数据时
    socket.on('update_online_count', function(online_stat){
        console.log(online_stat);
    });








</script>

<script type="text/javascript">  
function changes(url){
  var content=$.trim($('input[name=content]').val());
  $('#div').append('<p>send:  '+content+'</p>');
  $.ajax({
      type:'GET',
      dataType:'html',
      data:{content:content},
      url:url,
      success:function(data){
          $('input[name=content]').val('');
      }


  })
}
  </script>  
<?php
$content=isset($_GET['content']) ? $_GET['content'] : '111';
// 指明给谁推送，为空表示向所有在线用户推送
$to_uid = "333";
// 推送的url地址，使用自己的服务器地址
$push_api_url = "http://192.168.1.66:2121/";
$post_data = array(
   "type" => "publish",
   "content" => $content,
   "to" => $to_uid, 
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
$return = curl_exec ( $ch );
curl_close ( $ch );
var_export($return);
?>